<?

function getPlayerById($id){
	global $sql;
	$var=$sql->request_single("select * from ".CFG_MYSQL_PREFIX."player where id=".$id);
	if($var===false) return false;
	else return new player($var);
}


function rosterMAJPerso($nom, $rang=false, $sortie=false){
	$json=armory_getPlayer($nom);
	if($json==false){
		if($sortie) echo $_REQUEST["nom"].",nok";
	}else{
		$var=array();
		$var["id"]=0;
		$var["nom"]=$json->name;
		$var["level"]=$json->level;
		$var["race"]=$json->race;
		$var["classe"]=$json->class;
		$var["sexe"]=$json->gender;
		$var["guilde"]=$json->guild->name;
		if($node["guildName"]!==CFG_GUILDE) $rang=20;
		if($rang===false) $var["grade"]= rosterGetRank($node["name"],$node["guildName"]);
		else $var["grade"]=$rang;
				
		$var["metier1"]=$json->professions->primary[0]->name;
		$var["niveau1"]=$json->professions->primary[0]->rank;
		$var["metier2"]=$json->professions->primary[1]->name;
		$var["niveau2"]=$json->professions->primary[1]->rank;
		
		for($i=0;$i<2;$i++){
			if(isset($json->talents[$i])){
				if(isset($json->talents[$i]->selected)){
					$var["spe1"]=$json->talents[$i]->spec->role;
					$var["talents1"]=$json->talents[$i]->spec->name;
				}else{
					$var["spe2"]=$json->talents[$i]->spec->role;
					$var["talents2"]=$json->talents[$i]->spec->name;
				}
			}
		}
		
		$var["stuff"]="";
		if(isset($json->items->head))
		{
			if(isset($json->items->head->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->head->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->head->id.",";
		}
		if(isset($json->items->neck))
		{
			if(isset($json->items->neck->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->neck->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->neck->id.",";
		}
		if(isset($json->items->shoulder))
		{
			if(isset($json->items->shoulder->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->shoulder->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->shoulder->id.",";
		}
		if(isset($json->items->back))
		{
			if(isset($json->items->back->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->back->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->back->id.",";
		}
		if(isset($json->items->chest))
		{
			if(isset($json->items->chest->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->chest->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->chest->id.",";
		}
		if(isset($json->items->shirt))
		{
			if(isset($json->items->shirt->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->shirt->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->shirt->id.",";
		}
		if(isset($json->items->wrist))
		{
			if(isset($json->items->wrist->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->wrist->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->wrist->id.",";
		}
		if(isset($json->items->hands))
		{
			if(isset($json->items->hands->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->hands->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->hands->id.",";
		}
		if(isset($json->items->waist))
		{
			if(isset($json->items->waist->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->waist->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->waist->id.",";
		}
		if(isset($json->items->legs))
		{
			if(isset($json->items->legs->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->legs->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->legs->id.",";
		}
		if(isset($json->items->feet))
		{
			if(isset($json->items->feet->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->feet->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->feet->id.",";
		}
		if(isset($json->items->finger1))
		{
			if(isset($json->items->finger1->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->finger1->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->finger1->id.",";
		}
		if(isset($json->items->finger2))
		{
			if(isset($json->items->finger2->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->finger2->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->finger2->id.",";
		}
		if(isset($json->items->trinket1))
		{
			if(isset($json->items->trinket1->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->trinket1->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->trinket1->id.",";
		}
		if(isset($json->items->trinket2))
		{
			if(isset($json->items->trinket2->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->trinket2->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->trinket2->id.",";
		}
		if(isset($json->items->mainHand))
		{
			if(isset($json->items->mainHand->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->mainHand->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->mainHand->id.",";
		}
		if(isset($json->items->offHand))
		{
			if(isset($json->items->offHand->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->offHand->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->offHand->id.",";
		}
		if(isset($json->items->ranged))
		{
			if(isset($json->items->ranged->tooltipParams->transmogItem))
				$var["stuff"].=$json->items->ranged->tooltipParams->transmogItem.",";
			else
				$var["stuff"].=$json->items->ranged->id.",";
		}
		$var["stuff"]=substr($var["stuff"],0,-1);
		
		$var["xml"]=serialize($json);
		$now = new DateTime("now", new DateTimeZone("Europe/Paris"));
		$var["ts"]=$now->format('Y-m-d H:i:s');
		
		$player= new player($var);
		if($player->exists()) $player->maj();
		else $player->ajouter();

		if($sortie) echo $var["nom"].", ok";
	}
}

function rosterGetRank($nom,$guilde){
	$json=armory_getGuild($guilde);
	if($json===false) echo "nok";
	else{
//		$node = $xml->xpath("/page/guildInfo/guild/members/character");
		$node = $json->members;
		foreach($node as $char){
			if(strval($char->character->name)==strval($nom)){
				return $char->rank;
			}
		}
	}
}

function get_ico_metier($m){
	if($m=="Enchantement") return "http://eu.media.blizzard.com/wow/icons/18/trade_engraving.jpg";
	if($m=="Travail du cuir") return "http://eu.media.blizzard.com/wow/icons/18/trade_leatherworking.jpg";
	if($m=="Joaillerie") return "http://eu.media.blizzard.com/wow/icons/18/inv_misc_gem_01.jpg";
	if($m=="Alchimie") return "http://eu.media.blizzard.com/wow/icons/18/trade_alchemy.jpg";
	if($m=="Herboristerie") return "http://eu.media.blizzard.com/wow/icons/18/trade_herbalism.jpg";
	if($m=="Couture") return "http://eu.media.blizzard.com/wow/icons/18/trade_tailoring.jpg";
	if($m=="Ingénierie") return "http://eu.media.blizzard.com/wow/icons/18/trade_engineering.jpg";
	if($m=="Minage") return "http://eu.media.blizzard.com/wow/icons/18/inv_pick_02.jpg";
	if($m=="Forge") return "http://eu.media.blizzard.com/wow/icons/18/trade_blacksmithing.jpg";
	/* TODO: good graph needed */
	if($m=="Calligraphie") return "http://eu.media.blizzard.com/wow/icons/18/inv_misc_questionmark.jpg";
	if($m=="Dépeçage") return "http://eu.media.blizzard.com/wow/icons/18/inv_misc_pelt_wolf_01.jpg";
	return "http://eu.media.blizzard.com/wow/icons/18/inv_misc_questionmark.jpg";
}

function roster_userAPlayer($idUser,$idPlayer){
	global $sql;
	$var=$sql->request_single("select id from ".CFG_MYSQL_PREFIX."player where id=".$idPlayer." and idUser=".$idUser);
	return ($var!==false);
}

function roster_getUserPlayers($idUser){
	global $sql;
	$tmp=array();
	
	$res=$sql->request("select * from ".CFG_MYSQL_PREFIX."player where idUser=".$idUser);
	if(sizeof($res)==0) return false;
	foreach($res as $var){
		$tmp[]=new player($var);
	}
	return $tmp;
}